package com.example.fangqidian.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.fangqidian.R;

/**
 * 创建日期:2019/1/18 on 9:43
 * 描述：
 * 作者: zhougan
 */
public class ScrollImageView extends View {
    private static final String TAG = "ScrollImageView";
    private Bitmap mBackground;
    private boolean flag=true;

    public ScrollImageView(Context context) {
        super(context);
        init();
    }

    public ScrollImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public ScrollImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    public ScrollImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();

    }
    private void init(){
        mBackground= BitmapFactory.decodeResource(getResources(), R.drawable.image1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        moveBackground(canvas);

    }
    private int x;

    private void moveBackground(Canvas canvas){
        Log.i(TAG, "moveBackground: x="+x+",width:"+getWidth()+",height:"+getHeight());
        x-=10;
        int x2=mBackground.getWidth()-(-x);
        if(x2<=0){
            x=0;
            canvas.drawBitmap(mBackground,x,10,null);
        }else{
            canvas.drawBitmap(mBackground,x,10,null);
            canvas.drawBitmap(mBackground,x2,10,null);

        }
    }

    public void start(){
        Log.i(TAG, "start: 开始移动");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag){
                    postInvalidate();
                }
            }
        }).start();
    }


}
